package com.itheima.demo;

import java.util.Random;
import java.util.Scanner;

public class Test10 {
    public static void main(String[] args) {
        // 双色球-单式
        // 规则：投注号码由6个红色球号码和1个蓝色球号码组成。红色球号码从1-33中选择;蓝色球号码从1-16中选择。
        int[] redList = new int[7];// 从1-33中选择

        // 1.生成红色球号码 从1-33中选择
        int[] redData = createNumber(redList);
        // 2.生成蓝色球号码 从1-16中选择
        Random random = new Random();
        int blueNumber = random.nextInt(16) + 1;
        redData[redData.length - 1] = blueNumber;

        for (int redDatum : redData) {
            System.out.print(redDatum + " ");
        }

        System.out.println("=================================");

        int[] userList = userInputNumber();

        // 判断中奖情况
        int redCount = 0;// 记录中红球个数
        int blueCount = 0;// 记录中蓝球个数

        // 红色球个数
        for (int i = 0; i < userList.length - 1; i++) {
            for (int j = 0; j < redData.length - 1; j++) {
                if (userList[i] == redData[j]) {
                    redCount++;
                    break;
                }
            }
        }

        // 蓝色球个数
        if (userList[userList.length - 1] == redData[redData.length - 1]) {
            blueCount++;
        }

        if (redCount == 6 && blueCount == 1) {
            System.out.println("恭喜您中，一等奖，奖金为1000万元！！！");
        } else if (redCount == 6 && blueCount == 0) {
            System.out.println("恭喜您中，二等奖，奖金为500万元！！！");
        } else if (redCount == 5 && blueCount == 1) {
            System.out.println("恭喜您中，三等奖，奖金为3000元！！！");
        } else if ((redCount == 5 && blueCount == 0) || (redCount == 4 && blueCount == 1)) {
            System.out.println("恭喜您中，四等奖，奖金为200元！！！");
        }else if ((redCount == 3 && blueCount == 1) || (redCount == 2 && blueCount == 1)) {
            System.out.println("恭喜您中，五等奖，奖金为10元！！！");
        }else if ((redCount == 1 && blueCount == 1) || (redCount == 0 && blueCount == 1)) {
            System.out.println("恭喜您中，六等奖，奖金为5元！！！");
        }else {
            System.out.println("谢谢参与");
        }


    }

    // 获取用户输入的号码
    public static int[] userInputNumber() {
        int[] arr = new int[7];

        Scanner sc = new Scanner(System.in);
        // 红色球号码
        for (int i = 0; i < arr.length - 1; ) {
            System.out.println("请输入第" + (i + 1) + "个红球号码:");
            int redNumber = sc.nextInt();
            // 判断输入的号码是否在1-33中，且不重复
            if (redNumber >= 1 && redNumber <= 33) {
                // 判断是否已经存在
                boolean flag = isRed(arr, redNumber);
                if (!flag) {
                    arr[i] = redNumber;
                    i++;
                } else {
                    System.out.println(redNumber + "该号码已经存在,请重新输入");
                }
            } else {
                System.out.println("请输入有效的号码数字:");
            }
        }

        // 蓝色球号码
        System.out.println("请输入蓝色球号码:");
        // 判断蓝色球号码是否在1-16中
        while (true) {
            int blueNumber = sc.nextInt();
            if (blueNumber >= 1 && blueNumber <= 16) {
                arr[arr.length - 1] = blueNumber;
                break;
            } else {
                System.out.println("请输入有效的号码数字:");
            }
        }

        return arr;
    }

    // 获取红色球数字,且不重复
    public static int[] createNumber(int[] redList) {

        Random random = new Random();
        for (int i = 0; i < redList.length - 1; ) {
            int redNumber = random.nextInt(33) + 1;
            // 判断是否重复
            if (!isRed(redList, redNumber)) {
                redList[i] = redNumber;
                i++;
            }
        }
        return redList;
    }

    // 判断红色球号码是否重复
    public static boolean isRed(int[] redList, int number) {
        for (int i = 0; i < redList.length; i++) {
            if (redList[i] == number) {
                return true;
            }
        }
        return false;
    }


}
